<?php
use Think\Controller;
namespace Admin\Controller;


class FenleiController extends CommenController{

	public function index(){						//遍历数据
		$m = M('pathclass');
        $path1 = $m->order("fid = 0")->limit($page->firstRow.','.$page->listRows)->select();
        $path2 = $m->order("fid != 0")->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign("path1",$path1);
        $this->assign("path2",$path2);
    	$count = $m->count();
		$page = new \Think\Page($count,8);
		$show = $page->show();
    	$this->assign('page',$show);
        $this->display();
    }

    public function add(){
        $m=M("pathclass");
        $arr = $m->where("fid=0")->select();
        $this->assign("path",$arr);
        $this->display();
    }
	public function doadd(){						//添加分类

		/*$pathname = $_POST['name'];			//分类名
		$type = $_POST['type'];				//分类状态
		*/
		$m = M('pathclass');

		$data = $m->create();		//接收数据且新建

		$user = $data['name'];
		//dump($user);


		if($m -> where(array('name'=>$user))->select()){		//判断分类名是否重复
			$this->error('该分类已经存在,请重新添加');
		};
		
		
		if(!$data){
			$this ->error($model->geterror());
			exit;
		}

		if( $m->add() > 0){
			$this ->success('添加成功',U('index'));
		}else{
			$this->error('添加失败');
		}
	}

	public function edit(){
        $id = $_GET['id'];
        $m=M("pathclass");
        $path1 = $m->where("id = {$id}")->find();
        $this->assign("path1",$path1);
        $path2 = $m->where("fid = 0")->select();
        $this->assign("path2",$path2);
        $this->display();
    }

	public function doedit(){
		$id = $_GET['id'];
		$name = $_POST['name'];
		$display = $_POST['display'];
		//dump($id);
		$editlist = M('pathclass')->where("id={$id}")->save(array('name'=>$name,'display'=>$display));
		if($editlist){
			$this->success('修改分类成功',U('index'));
		}else{
			$this->error('修改失败');
		}
	}

	public function delete(){
		$id = $_GET['id'];
		//dump($id);
		$del = M('pathclass')->where("id={$id}")->delete();
		if($del > 0 ){
			$this->success('删除分类成功',U('index'));
		}else{
			$this->error('修改失败');
		}	
	}
}
